$(document).ready(function() {
	lang = $("#display-lang").val();
	ds = $("#dataset").val();
	showComments(0, 10, lang, true, ds);
});

function clickLike() {
	/* comment like dislike */
	var comments = new Array();
	var i = 0;
	$(".datasetComment").each(function() {
		comments.push($(this));
		i++;
	});

	for ( var x in comments) {
		var comment = comments[x];
		var cookieName = comment.attr("id") + $("#user").val();
		if ($.cookie(cookieName) != null && eval($.cookie(cookieName)) == true) {
			comment.children("a").attr("disabled", "true");
			comment.children("a").mouseover(function() {
				$(this).css("cursor", "default");
			});
		} else {
			comment.children("a").first().click(
					function() {
						var parent = $(this).parent(".datasetComment");
						var cookieName = parent.attr("id") + $("#user").val();
						var commentID = parent.attr("commentID");
						if ($.cookie(cookieName) == null
								|| eval($.cookie(cookieName)) == false) {
							$.cookie(cookieName, 'true', {
								expires : 1, path: '/'
							});
							insertCommentLike(commentID, $(this));
							parent.children("a").attr("disabled", "true");
							parent.children("a").mouseover(function() {
								$(this).css("cursor", "default");
							});
						}
					});

			comment.children("a").first().next().click(
					function() {
						var parent = $(this).parent(".datasetComment");
						var cookieName = parent.attr("id") + $("#user").val();
						var commentID = parent.attr("commentID");
						if ($.cookie(cookieName) == null
								|| eval($.cookie(cookieName)) == false) {
							$.cookie(cookieName, 'true', {
								expires : 1, path: '/'
							});
							insertCommentDisLike(commentID, $(this));
							parent.children("a").attr("disabled", "true");
							parent.children("a").mouseover(function() {
								$(this).css("cursor", "default");
							});
						}
					});
		}
	}
}

function insertCommentLike(commentID, el) {
	var data = {};
	data["id"] = commentID;
	data["like"] = "like";

	data = JSON.stringify(data);
	data = ("update=" + encodeURIComponent(data));
	$.ajax({
		url : "/sview/commentLikeDislike",
		dataType : "json",
		data : data,
		type : "post",
		success : function() {
			el.children("span").text(eval(el.children("span").text()) + 1);
		}
	});
}

function insertCommentDisLike(commentID, el) {
	var data = {};
	data["id"] = commentID;
	data["like"] = "dislike";

	data = JSON.stringify(data);
	data = ("update=" + encodeURIComponent(data));
	$.ajax({
		url : "/sview/commentLikeDislike",
		dataType : "json",
		data : data,
		type : "post",
		success : function() {
			el.children("span").text(eval(el.children("span").text()) + 1);
		}
	});
}

function showComments(start, length, lang, newFlag, ds) {
	var data = {};
	data["start"] = start;
	data["length"] = length;
	data["lang"] = lang;
	var ds_type = ds.substring(0, 1);
	if (ds_type == "c") {
		ds_type = "custom";
	} else {
		ds_type = "uri";
	}
	var ds_id = ds.substring(1);
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id;
	data = JSON.stringify(data);
	data = ("comment=" + encodeURIComponent(data));
	$
			.ajax({
				url : "/sview/datasetComments",
				async : true,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					var array = result["result"];
					var flag = false;
					if (newFlag) {
						$(".entity-list").html("");
					}
					if ($(".entity-list ol").length > 0)
						flag = true;
					if (!flag) {
						var olt = $("<ol></ol>");
						$(".entity-list").append(olt);
					}
					var ol = $(".entity-list>ol");
					for ( var i = 0; i < array.length; i++) {
						var li = $("<li></li>");
						ol.append(li);
						var element = array[i];
						li.addClass("lentity");
						var id = element["commentId"];
						var div = $('<div class="sc split"></div>');
						li.append(div);

						var commentBox = $("<div class='comment-box'></div>");
						commentBox.html(element["text"]);
						div.append(commentBox);
						var commentMetaSpan = $("<span  class='commentMeta'></span>");
						div.append(commentMetaSpan);
//						if(lang=="en"){
//							commentMetaSpan.html("by <b>" + element["author"]
//							+ "</b> on <b>" + element["time"] + "</b>");
//						}else if(lang=="zh"){
//							commentMetaSpan.html("作者：<b>" + element["author"]
//							+ "</b> 创建时间： <b>" + element["time"] + "</b>");
//						}else{
//							alert("lang="+lang);
//						}

						var dsCommentSpan = $("<span class='datasetComment' id='ds_comment_"
								+ id + "' commentID='" + id + "'></span>");
						div.append(dsCommentSpan);
						var aLike = $("<a href='javascript:void(0)' style='position: relative; left: 730px; text-decoration: none;' "
								+ "id='ds_comment_like_" + id + "'></a>");
						dsCommentSpan.append(aLike);
						aLike
								.append($("<img src='/sview/icons/like.png' style='width: 15px; border: 0;' />"));
						var likeTotal = $("<span style='font-size: xx-small; color: #1170A3;'></span>");
						likeTotal.html(element["likeTotal"]);
						aLike.append(likeTotal);
						var aDislike = $("<a href='javascript:void(0)' style='position: relative; left: 750px; text-decoration: none;' "
								+ "id='ds_comment_dislike_" + id + "'></a>");
						dsCommentSpan.append(aDislike);
						aDislike
								.append($("<img src='/sview/icons/dislike.png' style='width: 15px; border: 0;' />"));
						var dislikeTotal = $("<span style='font-size: xx-small; color: #1170A3;'></span>");
						dislikeTotal.html(element["dislikeTotal"]);
						aDislike.append(dislikeTotal);
					}
					$(".entity-list ol li:last>div").addClass("final");
					$(".more-container").remove();
					clickLike();
					if (array.length > 0) {
						var div = $("<div class='more-container'></div>");
						var moreDiv = $("<div class='moreDiv'></div>");
						var a = null;
						if (lang == 'en'){
							a = $('<a href="javascript:void(0)"><span class="tip">Load More</span><span class="arrowDown"></span></a>');
						}else if(lang=='zh'){
							a = $('<a href="javascript:void(0)"><span class="tip">更多</span><span class="arrowDown"></span></a>');
						}
							
						a.bind("click", function() {
							showComments(start + length, length, lang, false,
									ds);
						});
						moreDiv.append(a);
						div.append(moreDiv);
						$(".entity-list").parent().append(div);
					}
				}
			});
}